package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IUserTypeDao;
import com.cn.wanxi.model.UserTypeModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserTypeDaoImpl implements IUserTypeDao {
    @Override
    public List<UserTypeModel> getUserTypeList() {
        String sql = "select * from user_type";
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<UserTypeModel> list = new ArrayList<>();
        UserTypeModel userType = null;
            try {
                while (rs.next()) {
                    userType = new UserTypeModel();
                    userType.setId(rs.getInt("id"));
                    userType.setName(rs.getString("name"));
                    list.add(userType);
                }
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return list;
    }

    @Override
    public UserTypeModel getUserType(int id) {
        String sql = "select * from user_type where id=" +id;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        UserTypeModel userType = null;
        try {
            if (rs.next()) {
                userType = new UserTypeModel();
                userType.setId(rs.getInt("id"));
                userType.setName(rs.getString("name"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userType;
    }
}
